import time
from . import ConnPG
import psycopg2


class LiftDuty:


    async def user_lift_duty(self, user_id, duty_id):#解除职务关系

        #参数检查
        if not (user_id.isdecimal() and duty_id.isdecimal()):

            return {
                'status': -1,
                'errorCode': 105100,
                'errorMsg': '两个参数类型应为整数',
                'resultBody': None
            }

        user_id, duty_id = int(user_id), int(duty_id)
        
        check_sql = 'select * from user_duty where user_id=%s and duty_id=%s and drop_time is null'
        check_result = await ConnPG().fetchone(check_sql, (user_id, duty_id))

        if not check_result:

            return {
                'status': -1,
                'errorCode': 105102,
                'errorMsg': '该成员没有此职务，无法解除职务关系',
                'resultBody': None
            }

        sql = "update user_duty set drop_time=%s where id=%s"

        await ConnPG().execute(sql, (int(time.time()), check_result['id']))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "职务解除成功",
        }